/*
 * Notification.java
 *
 * 07/11/2009
 */
package kg.sound.player.ui;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author fernando
 */
public final class Notification {

	private static Notification instance;
	
	private final StringBuilder displayedMessages = new StringBuilder();
	
	private NotificationView view;
	
	private Notification() {
	}

	public static Notification getInstance() {
		if (instance == null) {
			instance = new Notification();
		}
		return instance;
	}
	
	public NotificationView getView() {
		if (view == null) {
			view = new NotificationView();
			view.addWindowListener(new WindowAdapter() {
				@Override
				public void windowClosed(WindowEvent e) {
					displayedMessages.setLength(0);
					view = null;
				}
			});
		}
		return view;
	}

	public void addMessage(String message) {
		//displayedMessages.insert(0, '\n').insert(0, message);
		displayedMessages.append(message).append('\n');
		
		getView().getNotificationArea().setText(displayedMessages.toString());
		getView().setVisible(true);
	}
	
	
	public static void main(String[] args) throws InterruptedException {
		for (int i = 1; i < 100; i++) {
			Thread.sleep(500L);
			getInstance().addMessage("Message " + i);
		}
	}
}
